package com.example.smartrefreshlayoutdemo.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.smartrefreshlayoutdemo.R;
import com.example.smartrefreshlayoutdemo.activity.FragmentActivity;
import com.example.smartrefreshlayoutdemo.activity.practice.BannerPracticeActivity;
import com.example.smartrefreshlayoutdemo.activity.practice.FeedListPracticeActivity;
import com.example.smartrefreshlayoutdemo.activity.practice.ProfilePracticeActivity;
import com.example.smartrefreshlayoutdemo.activity.practice.QQBrowserPracticeActivity;
import com.example.smartrefreshlayoutdemo.activity.practice.RepastPracticeActivity;
import com.example.smartrefreshlayoutdemo.activity.practice.WebViewPracticeActivity;
import com.example.smartrefreshlayoutdemo.activity.practice.WeiboPracticeActivity;
import com.example.smartrefreshlayoutdemo.adapter.BaseRecyclerAdapter;
import com.example.smartrefreshlayoutdemo.adapter.SmartViewHolder;
import com.example.smartrefreshlayoutdemo.fragment.practice.InstantPracticeFragment;
import com.example.smartrefreshlayoutdemo.fragment.practice.SecondFloorPracticeFragment;

import java.util.Arrays;

public class RefreshPracticeFragment extends Fragment implements AdapterView.OnItemClickListener {
    private enum Item{
        餐饮美食(R.string.index_practice_repast, RepastPracticeActivity.class),
        个人中心(R.string.index_practice_profile, ProfilePracticeActivity.class),
        WebView(R.string.index_practice_web_view, WebViewPracticeActivity.class),
        微博列表(R.string.index_practice_feed_list, FeedListPracticeActivity.class),
        微博(R.string.index_practice_weibo, WeiboPracticeActivity.class),
        Banner(R.string.index_practice_banner, BannerPracticeActivity.class),
        QQBrowser(R.string.index_practice_qq_browser, QQBrowserPracticeActivity.class),
        淘宝二楼(R.string.index_practice_second_floor, SecondFloorPracticeFragment.class),
        即时通讯(R.string.index_practice_instant, InstantPracticeFragment.class)
        ;
        @StringRes
        private int name;
        private Class<?> clazz;

        Item(int name, Class<?> clazz) {
            this.name = name;
            this.clazz = clazz;
        }
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_refresh_practice,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //StatusBarUtil.setPaddingSmart(getContext(),view.findViewById(R.id.toolbar));
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(new BaseRecyclerAdapter<Item>(Arrays.asList(Item.values()),android.R.layout.simple_list_item_2,this) {
            @Override
            public void onBindViewHolder(SmartViewHolder holder, Item model, int position) {
                holder.text(android.R.id.text1,model.name());
                holder.text(android.R.id.text2,model.name);
                holder.textColorId(android.R.id.text2,R.color.colorTextAssistant);
            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Item item = Item.values()[position];
        if (Activity.class.isAssignableFrom(item.clazz)) {
            startActivity(new Intent(getContext(),item.clazz));
        }else if(Fragment.class.isAssignableFrom(item.clazz)){
            FragmentActivity.start(this,item.clazz);
        }
    }
}
